/**  
 * 文 件 名: GroupsAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

/**
 * 
 */
package com.tongzhou.system.rightsMgmt.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.rightsMgmt.dao.IGroupsDAO;
import com.tongzhou.system.rightsMgmt.dao.IRightDAO;
import com.tongzhou.system.rightsMgmt.dao.IRoleDAO;
import com.tongzhou.system.rightsMgmt.model.Groups;
import com.tongzhou.system.rightsMgmt.model.Right;
import com.tongzhou.system.rightsMgmt.model.Role;
import com.tongzhou.system.rightsMgmt.model.SearchGroupsCondition;
import com.tongzhou.util.Util;

public class GroupsAction extends ActionSupport{
	private String id;//
	private String name;//群组名称
	private String active;//是否可用（Y，可用；N，不可用）
	private String create_time;//创建时间
	private String create_by;//创建人
	private String update_time;//更新时间
	private String update_by;//更新人
	
	private Groups groups;
	private List<Groups> result;
	private IGroupsDAO dao;
	private IRoleDAO roleDAO;
	private IRightDAO rightDAO;
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private SearchGroupsCondition condition;
	private String judgment;
	private List<Role> roleList;
	private List<Role> groupsRoleList;
	private List<Right> groupsRightList;
	private List<Right> rightList;
	private String itemsVal;
	private String rightStr;
	private Right right;
	
	public String listGroups() throws DBException{
		checkSearchCondition();
		condition = new SearchGroupsCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setName(name);
		condition.setActive(active);
		condition.setCurrentPage(currentPage);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType);
		condition.setPageSize(pageSize);
	    result = dao.listGroups(condition);
		return "listGroups";
	}
	
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "r.id";
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "asc" ;
		}
	}
	
	public String toAddGroups() throws DBException{
		return "toAddGroups";
	}
	
	public String checkGroupsByName() throws DBException{
		groups=new Groups();
		try {
			groups.setName(new String(name.getBytes("ISO-8859-1"),"UTF-8"));
			groups.setId(id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkGroupsByName(groups);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	public String addGroups() throws DBException{
		groups=new Groups();
		groups.setName(name);
		groups.setActive(active);
		groups.setCreate_by(String.valueOf(Util.getAdminUserId()));
		dao.addGroups(groups);
		listGroups();
		return "listGroups";
	}
	
	public String deleteGroups() throws DBException{
		groups=new Groups();
		groups.setId(id);
		
		dao.deleteGroups(groups);
		listGroups();
		name="";
		return "listGroups";
	}
	
	public String toEditGroups() throws DBException{
		groups=new Groups();
		groups.setId(id);
		groups=dao.getGroupsById(groups);
		return "toEditGroups";
	}
	
	public String editGroups() throws DBException{
		groups=new Groups();
		groups.setId(id);
		groups.setName(name);
		groups.setActive(active);
		groups.setUpdate_by(String.valueOf(Util.getAdminUserId()));
		dao.updateGroups(groups);
		listGroups();
		return "listGroups";
	}
	
	public String toEditGroupsRole() throws DBException{
		groups=new Groups();
		groups.setId(id);
		groupsRoleList=dao.getRoleByGroupsId(groups);
		
		roleList=roleDAO.getAllRole();
		return "toEditGroupsRole";
	}
	
	public String editGroupsRole() throws DBException{
		groups=new Groups();
		groups.setId(id);
		groups.setItemsVal(itemsVal);
		dao.updateGroupsRole(groups);
		listGroups();
		return "listGroups";
	}
	
	public String toEditGroupsRight() throws DBException{
		groups=new Groups();
		groups.setId(id);
		groupsRightList=dao.getGroupsRightById(groups);
		rightStr="";
		for(Right r:groupsRightList){
			if(rightStr!=""){
				rightStr+="`";
			}
			rightStr+=r.getId();
		}
		
		right=new Right();
		right.setId("0");
		List<Right> rr=rightDAO.getRightListByParentID(right);
		rightList=new ArrayList<Right>();
		for(Right r1:rr){
			Right r=getChildRight(r1);
			rightList.add(r);
		}
		return "toEditGroupsRight";
	}
	
	//查出子记录
	public Right getChildRight(Right r){
		List<Right> rlist=null;
		try {
			Right r1=new Right();
			r1.setParent_id(r.getId());
			rlist=rightDAO.getRightListByParentID(r1);
		} catch (DBException e) {
			e.printStackTrace();
		}
		if(rlist.size()>0){
			List<Right> rlist2=new ArrayList<Right>();
			for(Right sr:rlist){
				sr=getChildRight(sr);
				rlist2.add(sr);
			}
			r.setList(rlist2);
		}
		return r;
	}
	
	public String editGroupsRight() throws DBException{
		groups=new Groups();
		groups.setId(id);
		groups.setItemsVal(itemsVal);
		dao.updateGroupsRight(groups);
		listGroups();
		return "listGroups";
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getActive() {
		return active;
	}
	public void setActive(String active) {
		this.active = active;
	}
	public String getCreate_time() {
		return create_time;
	}
	public void setCreate_time(String createTime) {
		create_time = createTime;
	}
	public String getCreate_by() {
		return create_by;
	}
	public void setCreate_by(String createBy) {
		create_by = createBy;
	}
	public String getUpdate_time() {
		return update_time;
	}
	public void setUpdate_time(String updateTime) {
		update_time = updateTime;
	}
	public String getUpdate_by() {
		return update_by;
	}
	public void setUpdate_by(String updateBy) {
		update_by = updateBy;
	}

	public Groups getGroups() {
		return groups;
	}

	public void setGroups(Groups groups) {
		this.groups = groups;
	}

	public List<Groups> getResult() {
		return result;
	}

	public void setResult(List<Groups> result) {
		this.result = result;
	}

	public IGroupsDAO getDao() {
		return dao;
	}

	public void setDao(IGroupsDAO dao) {
		this.dao = dao;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getOrderByType() {
		return orderByType;
	}

	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}

	public SearchGroupsCondition getCondition() {
		return condition;
	}

	public void setCondition(SearchGroupsCondition condition) {
		this.condition = condition;
	}

	public String getJudgment() {
		return judgment;
	}

	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}

	public IRoleDAO getRoleDAO() {
		return roleDAO;
	}

	public void setRoleDAO(IRoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public List<Role> getGroupsRoleList() {
		return groupsRoleList;
	}

	public void setGroupsRoleList(List<Role> groupsRoleList) {
		this.groupsRoleList = groupsRoleList;
	}

	public String getItemsVal() {
		return itemsVal;
	}
	public IRightDAO getRightDAO() {
		return rightDAO;
	}

	public void setRightDAO(IRightDAO rightDAO) {
		this.rightDAO = rightDAO;
	}

	public List<Right> getGroupsRightList() {
		return groupsRightList;
	}

	public void setGroupsRightList(List<Right> groupsRightList) {
		this.groupsRightList = groupsRightList;
	}

	public List<Right> getRightList() {
		return rightList;
	}

	public void setRightList(List<Right> rightList) {
		this.rightList = rightList;
	}

	public String getRightStr() {
		return rightStr;
	}

	public void setRightStr(String rightStr) {
		this.rightStr = rightStr;
	}

	public Right getRight() {
		return right;
	}

	public void setRight(Right right) {
		this.right = right;
	}

	public void setItemsVal(String itemsVal) {
		this.itemsVal = itemsVal;
	}
}
